/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.android.camera.burst;

import java.util.ArrayList;
import java.util.List;

import com.android.camera.debug.Log;
import com.android.camera.one.v2.imagesaver.MetadataImage;
import com.android.camera.session.CaptureSession;

import android.content.ContentResolver;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.TotalCaptureResult;

/**
 * Stub implementation for burst controller.
 */
class BurstControllerImpl implements BurstController {
	private static final Log.Tag TAG = new Log.Tag("BurstControllerImpl");

	private BurstResultsListener mBurstResultsListener;
	
    public BurstControllerImpl(Context context) {}
    /**
     * Returns true if burst mode is supported by camera.
     */
    public static boolean isBurstModeSupported(ContentResolver contentResolver) {
        return true;
    }

    @Override
    public EvictionHandler startBurst(SurfaceTexture surfaceTexture,
            ImageStreamProperties imageStreamProperties,
            BurstResultsListener resultsListener,
            CaptureSession captureSession) {
    	mBurstResultsListener = resultsListener;
        return new EvictionHandler() {
        	final List<Long> frame = new ArrayList<Long>();
        	
			@Override
			public long selectFrameToDrop() {
				long timestamp = frame.remove(0);
				Log.d(TAG, "luxf selectFrameToDrop "+timestamp);
				return timestamp;
			}

			@Override
			public void onFrameCaptureResultAvailable(long timestamp, TotalCaptureResult captureResult) {
				Log.d(TAG, "luxf onFrameCaptureResultAvailable "+timestamp);
			}

			@Override
			public void onFrameInserted(long timestamp) {
				Log.d(TAG, "luxf onFrameInserted "+timestamp);
				frame.add(timestamp);
				if (frame.size() == 1) {
					mBurstResultsListener.onBurstStarted();
				}
			}

			@Override
			public void onFrameDropped(long timestamp) {
				Log.d(TAG, "luxf onFrameDropped "+timestamp);
			}
        	
        };
    }

    @Override
    public void processBurstResults(List<MetadataImage> capturedImages) {
    	if (capturedImages == null) return;
    	for (MetadataImage image : capturedImages) {
    		Log.d(TAG, "luxf processBurstResults "+image.getTimestamp());
    		Log.d(TAG, "luxf processBurstResults "+image.getWidth()+":"+image.getHeight());
    		image.close();
    	}
    	mBurstResultsListener.onBurstCompleted(null);
    }
}
